package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.DzjlModel;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/**
 * 动作记录详情页
 */
public class ActionRecordActivtity extends BaseActivity {

    private TitleView titleView;
    private InfoTextView tv_action_time, tv_action_evaluate, tv_reason,
            tv_duty_org, tv_action_descri, tv_action_report, tv_file, tv_boolean, tv_note;
    private Db db;
    private LinearLayout ll_dzjl;
    private DzjlModel model;
    private int i = -1;
    private DownAttachmentDialogUtil dialogUtil;


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dzjlxq);
        initviews();
        db = Db.getInstance(this);
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "动作记录");
        Log.e("1231", form_CODE);

        model = (DzjlModel) getIntent().getSerializableExtra("dzjl");
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");

        //动作时间
        tv_action_time.setText(model.action_time);
        //动作评价
        tv_action_evaluate.setText(model.appraise);

        tv_reason.setText(model.incorrect_reason);

        String visible1 = db.getVisible(form_CODE, "dept_name_editable");
        if (!visible1.equals("T")) {
            tv_duty_org.setVisibility(View.GONE);
        }
        tv_duty_org.setText(model.dept_name_editable);

        tv_action_descri.setText(model.action_describe);
        tv_note.setText(model.note);

//        String name2 = db.getName("WJMC","TB_FILE_LB","OBJ_ID",model.ttbglj);
        //动作报告
        //附件
        Log.e("tv_attachment", model.attachment);
        String name3 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        if (!StringUtils.isNullOrEmpty(name3)) {
            tv_file.setText(name3);
            i = 1;
        } else {
            String name = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            tv_file.setText(name);
            i = 2;
        }


        String visible2 = db.getVisible(form_CODE, "is_lubo_ok_editable");
        if (!visible2.equals("T")) {
            tv_boolean.setVisibility(View.GONE);
        }
        tv_boolean.setText(model.is_lubo_ok_editable);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_dzjl.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);

                //设置value的值
                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);
//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//----------------------------------------------------------------------------------------------------

    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);
        tv_action_time = (InfoTextView) findViewById(R.id.tv_action_time);
        tv_action_evaluate = (InfoTextView) findViewById(R.id.tv_action_evaluate);
        tv_reason = (InfoTextView) findViewById(R.id.tv_reason);
        tv_duty_org = (InfoTextView) findViewById(R.id.tv_duty_org);
        tv_file = (InfoTextView) findViewById(R.id.tv_file);
        tv_boolean = (InfoTextView) findViewById(R.id.tv_boolean);
        tv_action_descri = (InfoTextView) findViewById(R.id.tv_action_descri);
        ll_dzjl = (LinearLayout) findViewById(R.id.ll_dzjl);
        tv_note = (InfoTextView) findViewById(R.id.tv_note);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });

//        tv_action_report.setOnClickListener(new View.OnClickListener(
//        ) {
//            public void onClick(View view) {
//                Log.i("点击测试", "收到点击事件");
//                String filePath = AppConfig.ATTACHMENT_PATh + tv_action_report.getText();
//                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_action_report.getText());
//                Log.e("LLL", String.valueOf(nullOrEmpty));
//                if(nullOrEmpty)
//                    return;
//                File file = new File(filePath);
//                if (!file.exists()) {
//                    Toast.makeText(ActionRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
//                } else {
//                    Intent intent = MyIntent.getPdfFileIntent(filePath);
//                    startActivity(intent);
//                }
//
//            }
//        });

        tv_file.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_file.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(ActionRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(ActionRecordActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(ActionRecordActivtity.this, guid, model.attachment, fjlx).execute();
                                                dialogUtil.dismiss();
                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(ActionRecordActivtity.this, fjlx, filePath);
                    }
                }
            }
        });
    }
}